package com.cty._03_SolutionSeekingMindset._38_StringPermutation;

/**
 * @Auther: cty
 * @Date: 2020/7/14 14:32
 * @Description: 面试题38：字符串的排列
 * 题目：输入一个字符串，打印出该字符串中字符的所有排列。例如输入字符串abc，
 * 则打印出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。
 * @version: 1.0
 */
public class StringPermutation {

    public static void permutation(char[] string, int length){
        if(string==null || length==0)
            return;

        recPermutation(string, length, 0);
    }  // end permutation()

    private static void recPermutation(char[] string, int length, int begin){
        if(begin == length-1)
            System.out.println(string);
        else{
            for(int i=begin; i<length; i++){
                char temp = string[i];
                string[i] = string[begin];
                string[begin] = temp;

                recPermutation(string, length, begin+1);

                temp = string[i];
                string[i] = string[begin];
                string[begin] = temp;
            }  // end for
        }  // end else
    }  // end recPermutation()

}  // end class
